/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       sky.frag
 * Author:     karooolek
 * Created on: 2010-01-04
 *
 **********************************************************************************************************************/

STRINGIZE(

varying vec3 position;

uniform sampler2D skyGradient;
uniform float darkness;

uniform vec4 sunColor;
uniform float lightness;
uniform vec3 sunPosition;
uniform float sunSizeSin;
uniform float sunSizeCos;

void main()
{
    // get position on sky
    vec3 V = normalize(position);
    
    // get color from sky gradient map
    vec4 color = texture2D(skyGradient, vec2(0.5 * sunPosition.z + 0.5, V.z));
    
    // far sky darkness
    color.rgb *= 1.0 - darkness * max(0.0, dot(V, -sunPosition));
    
    // sun color and sky brightness
    float VdotSun = max(0.0, dot(V, sunPosition));
    if(VdotSun >= sunSizeCos && V.z >= 0.0)
    {
        color = vec4(V.z / sunSizeSin) + vec4(0.66667, 0.33333, 0.0, 1.0);
    }
    else
    {
        color += sunColor * lightness * pow(VdotSun, 100.0 / lightness);
    }
    
    gl_FragColor = color;
}

) // STRINGIZE
